package hc.j2me.util;

import hc.core.IConstant;
import hc.core.RootServerConnector;
import hc.core.util.StringUtil;
import hc.j2me.Starter;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class J2MEHttpUtil {

	/**
	 * 如果没有成功，则返回null
	 * @param url
	 * @return
	 */
	public static String getAjax(String url){
//		L.V = L.O ? false : LogManager.log("RC : " + url);
		Connection conn = null;
		HttpConnection httpconn;
		while(true){
			try{
				conn = Connector.open(url);
		    	
				if (conn instanceof HttpConnection) {
					httpconn = (HttpConnection) conn;
		
					int responseCode = httpconn.getResponseCode();
					if (responseCode == 200){
						break;
					}else if(responseCode == 301 || responseCode == 302){
						conn.close();
						url = httpconn.getHeaderField("Location");					
					}else{
						conn.close();
						return null;
					}
				}
			}catch (Exception e) {
				e.printStackTrace();
				try{
					conn.close();
				}catch (Exception e1) {
					
				}
				return null;
			}
		}
		InputStream in;
		try {
		    int expectedLength = (int)httpconn.getLength();
		    if(expectedLength == 0){
		    	//长度为0的特殊情形
			    conn.close();
			    return "";
		    }
			in = httpconn.openInputStream();
	    	int MAX_BLOCK_SIZE = 20 * 1024;
		    if(expectedLength == -1){
		    	expectedLength = 1024;
		    }else if(expectedLength > MAX_BLOCK_SIZE){
		    	expectedLength = MAX_BLOCK_SIZE;
		    }
		    byte[] buf = new byte[expectedLength];
		    int n;
		    int total = 0;
	
		    while ((n = in.read(buf, total, buf.length - total)) != -1) {
		    	total += n;
		    	if (total == buf.length) {
		    		// try to read one more character
		    		int c = in.read();
		    		if (c == -1)
		    			break; // EOF, we're done
		    		else {
		    			if(buf.length * 2 <= MAX_BLOCK_SIZE){
	    	    			// need more space in array.  Double the array, but don't make
	    	    			// it bigger than maxBytes.
	    	    			byte[] newbuf = new byte[buf.length * 2];
	    	    			System.arraycopy(buf, 0, newbuf, 0, buf.length);
	    	    			buf = newbuf;
	    	    			buf[total++] = (byte) c;
		    			}else{
		    				break;
		    			}
		    		}
		    	}
		    }
	    	
		    in.close();
		    conn.close();

		    return new String(buf, 0, total, IConstant.UTF_8);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getAjaxForSimu(String url, boolean isTCP){
    	Object simu = Starter.properties.get("_testSimu");
		final boolean isSimu = (simu != null && simu.equals(IConstant.TRUE));
		if(isSimu){
    		url = StringUtil.replace(url, "homecenter.mobi", "localhost:8080");//
    		url = StringUtil.replace(url, "call.php", "callsimu.php");//
    	}
//		return getAjax(url);
    	return (isTCP?RootServerConnector.getAjaxTCP(url, isSimu):getAjax(url));
    }
	
}
